package sysman;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import common.serialization.IWebText;
import common.serialization.XmlDataBean;
import common.service.BaseService;

public class ConfigService extends BaseService implements IConfigService {
	//获取数据库的所有表名
	public IWebText getAllTable() throws Exception {
		XmlDataBean root = new XmlDataBean();
		root.setNameSpace("tree");
		root.setId("0");
		
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		try {
			conn = this.getConnection();
			rs = conn.getMetaData().getTables(null, null, "%", new String[]{"TABLE"});
			while(rs.next()) {
				String tableName = rs.getString(3);
				XmlDataBean child = new XmlDataBean();
				child.setNameSpace("item");
				child.setId(tableName);
				child.setAttribute("text", tableName);
				root.appendChildXmlDataBean(child);
			}
		} catch(Exception e){
			e.printStackTrace();
			throw new Exception(e);
		}finally {
			this.close(rs, ps, conn);
		}
		return root;
	}
	
	//获取表的所有列信息
	public Map<String,Map<String,Object>> getAllColumn(String tableName) throws Exception {
		Map<String,Map<String,Object>> retMap = new HashMap<String,Map<String,Object>>();
		
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		try {
			conn = this.getConnection();
			rs = conn.createStatement().executeQuery("select * from " + tableName + " where 1=2 ");
			ResultSetMetaData rsmd = rs.getMetaData();
			int columnCount = rsmd.getColumnCount();
			for (int i = 1; i <= columnCount; i++) {
				Map<String,Object> rowMap = new HashMap<String,Object>();
				
				String colName = rsmd.getColumnName(i);//字段名
				int len = rsmd.getPrecision(i);//长度
				String colTypeName = rsmd.getColumnTypeName(i);//数据类型
				String colType = "S";
				if("VARCHAR".equals(colTypeName)) {//字符串
					
				} else if("INTEGER".equals(colTypeName)) {//整数
					colType = "I";
				} else if("DATETIME".equals(colTypeName)) {//日期：年月日时分秒
					colType = "D";
				}
				
				rowMap.put("TABLE_NAME", tableName.toUpperCase());
				rowMap.put("FIELD_NAME", colName);
				rowMap.put("FIELD_TYPE", colType);
				rowMap.put("FIELD_LEN", String.valueOf(len));
				retMap.put(colName, rowMap);
			}
				
		} catch(Exception e){
			e.printStackTrace();
			throw new Exception(e);
		}finally {
			this.close(rs, ps, conn);
		}
		return retMap;
	}
	
	//初始化配置信息
	@SuppressWarnings("unused")
	public void initTableConfig(String tableName, List<Map<String,Object>> pageList) throws Exception {
		Map<String,Map<String,Object>> columnMap = getAllColumn(tableName);
		
		for(Map<String,Object> map : pageList) {
			String columnName = (String)map.get("FIELD_NAME");
			if(columnMap.get(columnName) != null) {
				columnMap.remove(columnName);
			}
		}

		if(pageList == null) {
			pageList = new ArrayList<Map<String,Object>>();
		}
		
		//参数列表
		List<Object[]> parameterList = new ArrayList<Object[]>();
		
		Set<String> set = columnMap.keySet();
		Iterator<String> iter = set.iterator();
		while(iter.hasNext()) {
			String columnName = iter.next();
			Map<String,Object> map = columnMap.get(columnName);
			pageList.add(map);
			
			Object[] paraArr = new Object[]{map.get("TABLE_NAME"),map.get("FIELD_NAME"),map.get("FIELD_TYPE"),map.get("FIELD_LEN")};
			parameterList.add(paraArr);
		}
		
		if(!parameterList.isEmpty()) {
			executeBatchBySql("insert into SYSMAN_TABLE_CONFIG(TABLE_NAME,FIELD_NAME,FIELD_TYPE,FIELD_LEN) values(?,?,?,?)", parameterList);
		}	
	}

}
